package com.order.controller;


import com.alibaba.fastjson.JSON;
import com.order.entity.vo.UserVO;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.OAuth2Request;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zzq
 * @since 2021-08-24
 */
@RestController
public class TUserController {

    @GetMapping(value = "/o1")
    // @PreAuthorize("hasAuth ority('p1')")//拥有p1权限方可访问此url
    public String r1() {
        //获取用户身份信息
//        //UserVO userDTO = (UserVO) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//
//        return userDTO.getFullname() + "访问资源1";

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        OAuth2Authentication oAuth2Authentication = (OAuth2Authentication) authentication;
        Authentication userAuthentication = oAuth2Authentication.getUserAuthentication();
        String s = JSON.toJSONString(userAuthentication);
        System.out.println(s);
        String name = userAuthentication.getName();
        System.out.println(name);

        OAuth2Request oAuth2Request = oAuth2Authentication.getOAuth2Request();
        Map<String, String> requestParameters = oAuth2Request.getRequestParameters();
        String s1 = JSON.toJSONString(requestParameters);
        System.out.println(s1);

        UserVO principal = (UserVO) userAuthentication.getPrincipal();
        return principal.getFullname();
    }

    @GetMapping(value = "/o2")
    // @PreAuthorize("hasAuth ority('p1')")//拥有p1权限方可访问此url
    public String r2() {
        return LocalDateTime.now().toString();
    }


}
